# !/usr/bin/python3
# -*- coding: utf-8 -*-
import json
import requests
from bs4 import BeautifulSoup


class XueqiuSpider:
    def __init__(self, page, max_page):
        self.page = page
        self.max_page = max_page
        self.base_url = "https://xueqiu.com/service/v5/stock/screener/quote/list?page={}" \
                        "&size=30&order=desc&order_by=percent&exchange=CN&market=CN&type=sha&_=1617689439257"
        # https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=percent&exchange=CN&market=CN&type=sza&_=1617697455454
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/70.0.3538.110 Safari/537.36 "
        }
        pass

    def get_url_list(self):
        url_list = []

        for pn in range(1, self.max_page, 1):
            url = self.base_url.format(pn)
            url_list.append(url)
        return url_list

    def get_content(self, url):
        response = requests.get(url=url, headers=self.headers)
        return response.content

    def run(self):
        url_list = self.get_url_list()
        for url in url_list:
            content = self.get_content(url)
            self.get_items(content)
        # self.get_detail('SH605378')
        pass

    def get_items(self, content):
        html = str(content, 'utf-8')
        json_str = json.loads(html)
        for data_list in json_str['data']['list']:
            print('data_list= ', data_list)
            # 股票代码
            symbol = data_list['symbol']
            # 股票名称
            name = data_list['name']
            # 当前价
            current = data_list['current']
            # 涨跌额
            chg = data_list['chg']
            # 涨跌幅
            percent = data_list['percent']
            # 年初至今
            current_year_percent = data_list['current_year_percent']
            # 成交量
            volume = data_list['volume']
            # 成交额
            amount = data_list['amount']
            # 换手率
            turnover_rate = data_list['turnover_rate']
            # 市盈率
            pe_ttm = data_list['pe_ttm']
            # 股息率
            dividend_yield = data_list['dividend_yield']
            # 市值
            market_capital = data_list['market_capital']
            print('股票代码: ' + symbol + ' 股票名称 ' + name + ' 当前价 ' + str(current) + ' 涨跌额 ' + str(chg) + ' 涨跌幅 '
                  + str(percent) + '%' + ' 年初至今 ' + str(current_year_percent) + '%' + ' 成交量 ' + str(volume) + ' 成交额 '
                  + str(amount) + ' 换手率 ' + str(turnover_rate) + '%' + ' 市盈率 ' + str(pe_ttm) + ' 股息率 '
                  + str(dividend_yield) + '%' + ' 市值 ' + str(market_capital))
            self.get_detail(symbol)

    def get_detail(self, symbol):
        # https://xueqiu.com/S/SH605378
        base_detail_url = 'https://xueqiu.com/S/'
        content = self.get_content(base_detail_url + symbol)
        html = str(content, 'utf-8')
        soup = BeautifulSoup(html, 'lxml')
        # print('soup ', soup)
        name = soup.find(class_='stock-name').string
        current = soup.find(class_='stock-current').string
        change = soup.find(class_='stock-change').string
        rises = soup.findAll(class_='stock-rise')
        length = len(rises)

        # rise0 = rises[0]
        # rise1 = rises[1].string
        # rise2 = rises[2].string
        # rise3 = rises[3].string
        # rise4 = rises[4].string
        # rise5 = rises[5].string
        # rise6 = rises[6].string
        print('name ', name, 'current ', current, 'change ', change)
        # print('rise0 ', rise0)
        # print('最高 ', rise1)
        # print('今开 ', rise2)
        # print('涨停 ', rise3)
        # print('最低 ', rise4)
        # print('量比 ', rise5)
        # print('委比 ', rise6)
        print('length ', length, 'rises ', rises)
        pass


if __name__ == '__main__':
    spider = XueqiuSpider(1, 2)
    spider.run()
